<template>
  <div v-loading.fullscreen="loading">
    <el-row :gutter="10">
      <el-col
        :span="24"
        style="text-align:left"
      >
        <el-radio-group v-model="dialogDatas.filters.applyType">
          <el-radio
            :value="4"
            border
          >回温</el-radio>
          <el-radio
            :value="5"
            border
          >搅拌</el-radio>
          <el-radio
            :value="6"
            border
          >搅拌结束</el-radio>
          <el-radio
            :value="1"
            border
          >开封</el-radio>
          <el-radio
            :value="2"
            border
          >冷藏</el-radio>
          <el-radio
            :value="3"
            border
          >用完</el-radio>
        </el-radio-group>
      </el-col>

      <el-col
        :span="24"
        style="text-align:left"
        class="marginTop10"
      >
        <el-input
          v-model="dialogDatas.filters.applyno"
          size="large"
          @change="dialogDatas.confirmBtnClick"
          clearable
        >
          <template #prepend>
            <span>物料编码</span>
          </template>
        </el-input>
      </el-col>

    </el-row>

  </div>
</template>
<script>
import { computed, reactive, ref } from 'vue';
import { onMounted, watch } from '@vue/runtime-core';
import { dayjs } from 'element-plus';

import myAxios from '@/myAxios/myAxios';
import myAxiosTPX from '@/myAxios/myAxiosTPX';
import { showWarning } from '@/hooks/useMyMessageBox';
import myAxios193 from '@/myAxios/myAxios193';
import { baseUrl } from '@/myAxios/api.config';
import store from '@/store';

export default {
  name: 'XgRecordView',
  setup () {
    let loading = ref(false);

    let dialogDatas = reactive({
      filters: {
        applyType: 4,
        applyno: '',
      },

      dialogFormVisible: false,
      currentData: {},
      isNew: false,
      confirmBtnClick: async function () {
        if (!dialogDatas.filters.applyno) {
          showWarning('请输入物料编码');
          return;
        }
        loading.value = true;
        let selectParams = `${dialogDatas.filters.applyno}🈹`;
        let matDatas = await myAxios.get('陈帅廷查询锡膏记录', selectParams);
        // 检查当前状态是否可以正常变更
        let msg = dialogDatas.checkState(matDatas);
        if (msg) {
          loading.value = false;
          showWarning(msg);
          return;
        }
        let now = dayjs().format('YYYY/MM/DD HH:mm:ss');
        // QRCODE,
        //       '{1}' AS STATE,
        //       '{2}' AS OPENTIME,
        //       '{3}' AS CLOSETIME,
        //       '{4}' AS ENDTIME
        let mergeParams = '';

        const applyType = dialogDatas.filters.applyType;
        const firstMatData = matDatas[0] || {};
        const qrcode = dialogDatas.filters.applyno;
        let OPENTIME = firstMatData['OPENTIME'] || '';
        let CLOSETIME = firstMatData['CLOSETIME'] || '';
        let ENDTIME = firstMatData['ENDTIME'] || '';

        mergeParams = [
          qrcode,
          applyType,
          applyType === 1 ? now : OPENTIME,
          applyType === 2 ? now : CLOSETIME,
          applyType === 3 ? now : ENDTIME,
        ].join('🈹');

        let postName = '陈帅廷添加或修改锡膏记录';

        //如果是回温
        if (applyType == 4) {
          let params = `${qrcode}🈹${matDatas[0].HWCOUNT + 1}`;
          postName = '陈帅廷修改锡膏回温次数';
          mergeParams = params;
          let hwParams = `${qrcode}🈹回温🈹${now}🈹`;
          myAxios.post('陈帅廷添加或修改锡膏回温记录', hwParams).then(res => {
            console.log(`陈帅廷添加或修改锡膏回温记录 ` + res);
          });
        }
        else if (applyType == 5) {
          myAxios.post('陈帅廷更新锡膏回温记录回温结束时间', `${qrcode}🈹回温🈹${now}`).then(res => {
            console.log(`陈帅廷更新锡膏回温记录回温结束时间 ` + res);
          });

          let jbParams = `${qrcode}🈹搅拌🈹${now}🈹`;
          myAxios.post('陈帅廷添加或修改锡膏回温记录', jbParams).then(res => {
            console.log(`陈帅廷添加或修改锡膏回温记录 ` + res);
          });
          loading.value = false;
          ElMessage({
            showClose: true,
            type: 'success',
            message: '提交成功!',
          });
          dialogDatas.filters.applyno = '';
          return;
        }
        else if (applyType == 6) {

          myAxios.post('陈帅廷更新锡膏回温记录回温结束时间', `${qrcode}🈹搅拌🈹${now}`).then(res => {
            console.log(`陈帅廷更新锡膏回温记录回温结束时间 ` + res);
          });
          loading.value = false;
          ElMessage({
            showClose: true,
            type: 'success',
            message: '提交成功!',
          });
          dialogDatas.filters.applyno = '';
          return;
        }

        myAxios.post(postName, mergeParams).then(async res => {
          if (res.includes('成功')) {
            if ([1, 2, 3, 4].includes(applyType)) {
              let dataContent = {
                "timerType": 'xg',
                "qrcode": qrcode,
                //0:开封/冷藏/用完，1:回温
                "state": [1, 2, 3].includes(applyType) ? 0 : 1,
                //0:开始,1:结束
                "type": [1, 4].includes(applyType) ? 0 : 1,
              };
              await myAxios193.post('tpx/postXGTimer', JSON.stringify(dataContent));
            }
            loading.value = false;

            ElMessage({
              showClose: true,
              type: 'success',
              message: '提交成功!',
            });
            dialogDatas.filters.applyno = '';

          }
          else {
            loading.value = false;
            showWarning('更新失败res!' + res);
          }
        }).catch((err) => {
          loading.value = false;
          showWarning('更新失败err!' + err);
        });

      },
      checkState (matDatas) {
        let msg = '';
        return msg;
        if (matDatas.length == 0) {
          msg = '';
          return msg;
        }
        let state = matDatas[0].STATE;
        // 回温
        if (dialogDatas.filters.applyType == 4) {
          if ([1].includes(state)) {
            if (matDatas[0].HWCOUNT < 2) {
              msg = '';
            }
            else {
              msg = '该物料回温次数超过2次，不能再次回温';
            }
          }
          else {
            msg = '该物料未开封，不能回温';
          }
        }
        // 搅拌
        if (dialogDatas.filters.applyType == 5) {
          if ([1].includes(state)) {
            msg = '';
          }
          else {
            msg = '该物料未开封，不能搅拌';
          }
        }
        // 搅拌结束
        if (dialogDatas.filters.applyType == 6) {
          if ([1].includes(state)) {
            msg = '';
          }
          else {
            msg = '该物料未开封，不能结束搅拌';
          }
        }
        // 开封、冷藏、用完
        if (dialogDatas.filters.applyType == 1) {
          if ([0, 2].includes(state)) {
            msg = '';
          }
          else if (state == 1) {
            msg = '该物料已开封，不能再次开封';
          }
          else if (state == 3) {
            msg = '该物料已用完，不能再次开封';
          }
        }
        else if (dialogDatas.filters.applyType == 2) {
          if ([0, 1].includes(state)) {
            msg = '';
          }
          else if (state == 2) {
            msg = '该物料已冷藏，不能再次冷藏';
          }
          else if (state == 3) {
            msg = '该物料已用完，不能再次冷藏';
          }
        }
        else if (dialogDatas.filters.applyType == 3) {
          if ([0, 1, 2].includes(state)) {
            msg = '';
          }
          else if (state == 3) {
            msg = '该物料已用完，不能再次用完';
          }
        }
        return msg;
      },
      cancelBtnClick: function () {

      }
    });

    return {
      loading,
      dialogDatas,
    };
  }
}
</script>
<style lang="css" scope>
.warning-row {
  background-color: var(--el-color-warning) !important;
}
.marginTop10 {
  margin-top: 10px;
}
</style>

